<?php
// +----------------------------------------------------------------------
// | 基础方法
// +----------------------------------------------------------------------
namespace app\common\service;

use app\common\model\SysUserRoleModel;
use app\common\service\SysRoleMenuService;

class SysUserRoleService{

    /**
     * 新增
     * @param  array  $data  新增的数据
     * @return int
     */
    public static function save($data){
        $res = SysUserRoleModel::create($data);
        return $res->id;
    }

    /**
     * 批量新增
     * @param  array  $list  新增的数据
     * @return int
     */
    public static function saveAll($list){
        $model = new SysUserRoleModel;
        $model->saveAll($list);
    }

    /**
     * 删除
     * @param  string  $userId  用户ID
     */
    public static function destroyByUserId($userId){
        if(!empty($userId)){
            SysUserRoleModel::where('user_id', $userId)->update(['delete_time' => date('Y-m-d H:i:s',time())]);
        }
    }

    /**
     * 列表
     * @param  array  $param  查询参数
     */
    public static function list($param = []){
        $model = new SysUserRoleModel;
        if (isset($param['user_id']) && !empty($param['user_id'])) {
            $model = $model->where('user_id', $param['user_id']);
        }
        $userRoleList = $model->select()->toArray();
        if (empty($userRoleList)) {
            return [];
        }
        $roleIds = array_column($userRoleList, 'role_id');
        $list = SysRoleMenuService::listByRoleIds($roleIds);

        return $list;
    }

    /**
     * 列表
     * @param  string  $userId  用户ID
     */
    public static function getListByUserId($userId){
        return SysUserRoleModel::where('user_id', $userId)->select()->toArray();
    }
}